home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / example / SoundArea.java < prev   
Encoding:
Java Source  |  1997-07-13  |  4.6 KB  |  141 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. /*
  18.  * @(#)SoundArea.java    1.3 95/10/13  
  19.  *
  20.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  21.  *
  22.  * Permission to use, copy, modify, and distribute this software
  23.  * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  24.  * without fee is hereby granted. 
  25.  * Please refer to the file http://java.sun.com/copy_trademarks.html
  26.  * for further important copyright and trademark information and to
  27.  * http://java.sun.com/licensing.html for further important licensing
  28.  * information for the Java (tm) Technology.
  29.  * 
  30.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  31.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  32.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  33.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  34.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  35.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  36.  * 
  37.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  38.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  39.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  40.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  41.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  42.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  43.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  44.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  45.  * HIGH RISK ACTIVITIES.
  46.  */
  47.  
  48. import java.awt.Graphics;
  49. import java.applet.AudioClip;
  50. import java.net.URL;
  51. import java.net.MalformedURLException;
  52.  
  53. /**
  54.  * An audio feedback ImageArea class.
  55.  * This class extends the basic ImageArea Class to play a sound when
  56.  * the user enters the area.
  57.  *
  58.  * @author     Jim Graham
  59.  * @author     Chuck McManis
  60.  * @version     1.3, 13 Oct 1995
  61.  */
  62. class SoundArea extends ImageMapArea {
  63.     /** The URL of the sound to be played. */
  64.     URL sound;
  65.     AudioClip soundData = null;
  66.     boolean hasPlayed; 
  67.     boolean isReady = false;
  68.     long    lastExit = 0;
  69.     final static int HYSTERESIS = 1500;
  70.  
  71.     /**
  72.      * The argument is the URL of the sound to be played.
  73.      */
  74.     public void handleArg(String arg) {
  75.     try {
  76.         sound = new URL(parent.getDocumentBase(), arg);
  77.     } catch (MalformedURLException e) {
  78.         sound = null;
  79.     }
  80.     hasPlayed = false;
  81.     }
  82.  
  83.     /**
  84.      * The applet thread calls the getMedia() method when the applet
  85.      * is started.
  86.      */
  87.     public void getMedia() {
  88.     if (sound != null) {
  89.         soundData = parent.getAudioClip(sound);
  90.     }
  91.     if (soundData == null) {
  92.         System.out.println("SoundArea: Unable to load data "+sound);
  93.     }
  94.     isReady = true;
  95.     }
  96.  
  97.     /**
  98.      * The enter method is called when the mouse enters the area.
  99.      * The sound is played if the mouse has been outside of the
  100.      * area for more then the delay indicated by HYSTERESIS.
  101.      */
  102.     public boolean enter() {
  103.     // is the sound sample loaded?
  104.     if (! isReady) {
  105.         parent.showStatus("Loading media file...");
  106.         return false;
  107.     }
  108.  
  109.     /*
  110.       * So we entered the selection region, play the sound if
  111.      * we need to. Track the mouse entering and exiting the
  112.      * the selection box. If it doesn't stay out for more than
  113.      * "HYSTERESIS" millis, then don't re-play the sound.
  114.      */
  115.     long now = System.currentTimeMillis();
  116.     if (Math.abs(now - lastExit) < HYSTERESIS) {
  117.         // if within the window pretend that it was played.
  118.         hasPlayed = true;
  119.             return false;
  120.     }
  121.  
  122.     // Else play the sound.
  123.     if (! hasPlayed && (soundData != null)) {
  124.         hasPlayed = true;
  125.         soundData.play();
  126.     }
  127.  
  128.     return false;
  129.     }
  130.  
  131.     /**
  132.      * The exit method is called when the mouse leaves the area.
  133.      */
  134.     public void exit() {
  135.     if (hasPlayed) {
  136.         hasPlayed = false;
  137.         lastExit = System.currentTimeMillis(); // note the time of exit
  138.     }
  139.     }
  140. }
  141.